package rkdwogus7.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.ImageIcon;

import java.awt.GridLayout;

import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.CardLayout;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTabbedPane;
import javax.swing.JRadioButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtUid;
	private JTextField txtPwd;
	private JTextField textField;
	private final CardLayout cards = new CardLayout(0,0);  // 1
	private JPanel myPane = new JPanel();
	private JTabbedPane mainPane = new JTabbedPane(JTabbedPane.TOP);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AComp dialog = new AComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AComp() {
		setTitle(":: 고급 컴퍼넌트 ::");
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel top = new JPanel();
			top.setPreferredSize(new Dimension(10, 120));
			contentPanel.add(top, BorderLayout.NORTH);
			top.setLayout(new BorderLayout(0, 0));
			{
				JLabel topLogo = new JLabel("-");
				topLogo.setPreferredSize(new Dimension(400, 15));
				topLogo.setIcon(new ImageIcon(AComp.class.getResource("/rkdwogus7/swing/images/background/top.jpg")));
				top.add(topLogo, BorderLayout.WEST);
			}
			{
				final JPanel cardPane = new JPanel();
				cardPane.setPreferredSize(new Dimension(210, 10));
				// 여기 대충 210 정도
				top.add(cardPane, BorderLayout.EAST);
				cardPane.setLayout(cards); // 2
				// 이거 하면 1로 넘어가게 돼. 1에 final로 선언하면 돼.
				{
					JPanel loginPane = new JPanel();
					cardPane.add(loginPane, "login");
					
					txtUid = new JTextField();
					txtUid.setText("아이디");
					txtUid.setColumns(10);
					
					JButton loginBtn = new JButton("로그인");
					loginBtn.addActionListener(new ActionListener() {
						private Object myPane;

						public void actionPerformed(ActionEvent e) {
							// uid가 abc이고 pwd가 12345일 때 로그인 되게 하는 것
							if ( txtUid.getText().equals("")
									|| txtPwd.getText().equals("") ) {
								JOptionPane.showMessageDialog(null, "아이디나 비밀번호를 입력하세요");
							}
							else if ( txtUid.getText().equals("abc")
									&& txtPwd.getText().equals("12345") ) {
							cards.show(cardPane, "logout");
							// myPane.setEnabled(false);
							mainPane.setEnabledAt(2, true);
							// 탭 활성 / 비활성은 setEnabledAt() 을 이용
							// 3번째 탭인데 0번부터 시작이니 2를 쓰는 거야.
							}
							else {
								JOptionPane.showMessageDialog(null, "아이디와 비밀번호를 확인하세요!");
							}
							// cards : 카드레이아웃
							// cardPane : 카드레이아웃이 적용된 패널
							// logout : cardPane에 보여 줄 패널 이름
						}
					});
					
					txtPwd = new JTextField();
					txtPwd.setText("비밀번호");
					txtPwd.setColumns(10);
					
					JButton joinBtn = new JButton("회원가입");
					GroupLayout gl_loginPane = new GroupLayout(loginPane);
					gl_loginPane.setHorizontalGroup(
						gl_loginPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_loginPane.createSequentialGroup()
								.addContainerGap()
								.addGroup(gl_loginPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_loginPane.createSequentialGroup()
										.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(loginBtn))
									.addGroup(gl_loginPane.createSequentialGroup()
										.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addGap(7)
										.addComponent(joinBtn, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					);
					gl_loginPane.setVerticalGroup(
						gl_loginPane.createParallelGroup(Alignment.TRAILING)
							.addGroup(Alignment.LEADING, gl_loginPane.createSequentialGroup()
								.addContainerGap()
								.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
									.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(loginBtn))
								.addGap(18)
								.addGroup(gl_loginPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_loginPane.createSequentialGroup()
										.addGap(1)
										.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addComponent(joinBtn))
								.addContainerGap(46, Short.MAX_VALUE))
					);
					loginPane.setLayout(gl_loginPane);
				}
				{
					JPanel logoutPane = new JPanel();
					cardPane.add(logoutPane, "logout");
					
					JLabel myAvatar = new JLabel("New label");
					myAvatar.setIcon(new ImageIcon(AComp.class.getResource("/rkdwogus7/swing/images/splash/1407742524_cheshire_cat.png")));
					
					JLabel myNick = new JLabel("천송이");
					
					JButton logoutBtn = new JButton("로그아웃");
					logoutBtn.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							cards.show(cardPane, "login");
							mainPane.setEnabledAt(2,false);
						}
					});
					GroupLayout gl_logoutPane = new GroupLayout(logoutPane);
					gl_logoutPane.setHorizontalGroup(
						gl_logoutPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addContainerGap()
								.addComponent(myAvatar, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(27)
										.addComponent(myNick))
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(logoutBtn)))
								.addContainerGap(39, Short.MAX_VALUE))
					);
					gl_logoutPane.setVerticalGroup(
						gl_logoutPane.createParallelGroup(Alignment.TRAILING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addContainerGap()
								.addComponent(myAvatar, GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
								.addGap(27))
							.addGroup(Alignment.LEADING, gl_logoutPane.createSequentialGroup()
								.addGap(31)
								.addComponent(myNick)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(logoutBtn)
								.addContainerGap(41, Short.MAX_VALUE))
					);
					logoutPane.setLayout(gl_logoutPane);
				}
			}
		}
		
		// JTabbedPane mainPane = new JTabbedPane(JTabbedPane.TOP); // 얘도 맨 위로 뺄 거야
		contentPanel.add(mainPane, BorderLayout.CENTER);
		
		JPanel basicFind = new JPanel();
		mainPane.addTab("기본검색", new ImageIcon(AComp.class.getResource("/rkdwogus7/swing/images/menu/close.png")), basicFind, null);
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
		basicFind.add(rdbtnNewRadioButton);
		
		JPanel detailFind = new JPanel();
		mainPane.addTab("상세검색", new ImageIcon(AComp.class.getResource("/rkdwogus7/swing/images/menu/exit.png")), detailFind, null);
		
		textField = new JTextField();
		detailFind.add(textField);
		textField.setColumns(10);
		
		// JPanel myPane = new JPanel(); // 얘를 위로 쭉 빼 낼 거야.
		mainPane.addTab("My page", new ImageIcon(AComp.class.getResource("/rkdwogus7/swing/images/menu/new.png")), myPane, null);
		mainPane.setEnabledAt(2, false);
		// 종료 하면 다시 안보이게 만드는 것
		
		JButton btnNewButton = new JButton("New button");
		myPane.add(btnNewButton);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
